package com.huanke.iot.api.controller;
import com.huanke.iot.api.controller.h5.req.HistoryDataRequest;
import com.huanke.iot.api.controller.h5.response.SensorDataVo;
import com.huanke.iot.base.api.ApiResponse;
import com.huanke.iot.base.cache.Cache;
import com.huanke.iot.base.constant.RetCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author AAJ
 * @version 1.0
 * @date 2020/11/16
 */
@RestController
@Slf4j
@RequestMapping("/cache")
public class CacheController {

    @Value("${cacheKey}")
    private String cacheKey;

    @GetMapping("/getAll")
    public ApiResponse<List<Object>> getAllCache() {
        if (Cache.getAll() == null){
            log.info("空");
            return new ApiResponse<>();
        }else {
            List cacheList = new ArrayList();
            cacheList.add(Cache.getAll());
            log.info("Cache = {}",Cache.getAll());
            return new ApiResponse<>(cacheList);
        }
    }

    @PostMapping("/removeAll")
    public ApiResponse removeAll(@RequestParam("key") String key) {
        if (key != null && cacheKey.equals(key)){
            Cache.removeAll();
            return new ApiResponse<>(RetCode.OK,"删除成功");
        }else {
            return new ApiResponse<>(RetCode.ERROR,"删除失败");
        }
    }

}
